package com.fy56.platform.logistics.restful.api.utils;

import com.fy56.platform.logistics.utils.DateUtil;
import com.fy56.platform.logistics.utils.StringUtil;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 微信通知 formId 工具类
 */
public class WeChatFormIdUtil {

    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private static String FORMID_OBJECT_SPLIT = ",";
    private static String FORMID_COLUMN_SPLIT = "&_&";

    public  static String getUserNewFormIds(String oldFormdIds, String currentFormId, String openId){
        String result = "";
        if(StringUtil.isNotEmpty(currentFormId)){
            String currentTime = simpleDateFormat.format(new Date());
            currentFormId = openId + FORMID_COLUMN_SPLIT + currentFormId + FORMID_COLUMN_SPLIT + currentTime + FORMID_COLUMN_SPLIT + "0";

            // 判断oldFormdIds信息后，追加新formId
            result = (StringUtil.isNotEmpty(oldFormdIds)? (oldFormdIds + ","):"") + currentFormId;
        }
        // 判读当 currentFormId，若oldFormdIds不等于null 则返回
        if(StringUtil.isEmpty(currentFormId)){
            result = StringUtil.isNotEmpty(oldFormdIds)? oldFormdIds :"";
        }
        return result;
    }

    /**
     * 获取当前vipFormIds串中 可用的formId
     *
     * @param vipFormIds
     * @return
     */
    public static String[] getVipFormId(String vipFormIds, int dayNumber,  int sizeNumber) throws Exception{
        String[] resultArray = new String[]{};
        String resultFormId = "", resultOpenId = "",vipFormIdArray = "" ;
        try{
            if(StringUtil.isNotEmpty(vipFormIds)){
                // 分隔获取FormID数组
                String[] formIdArrays = vipFormIds.split(FORMID_OBJECT_SPLIT);
                if(formIdArrays != null && formIdArrays.length > 0){
                    // 循环数据，查找可用的FORMID
                    for (String formIdObj : formIdArrays) {
                        String[] formIdColumnArray = formIdObj.split(FORMID_COLUMN_SPLIT);
                        if(formIdColumnArray.length == 4){
                            String openId     = formIdColumnArray[0];
                            String formId     = formIdColumnArray[1];
                            String createTime = formIdColumnArray[2];
                            String useNumber  = formIdColumnArray[3];
                            int day = getTimeDay(createTime);
                            int count = getUseNumber(useNumber);

                            // 判断若返回resultFormId为空值时，执行下列操作, 提取有效的 formId，信息用户发送通知
                            if(StringUtil.isEmpty(resultFormId)){
                                // 若formId产生时间为5天内，且使用发送通知次数小于三次时，返回当前formId
                                if(day <= dayNumber && count < sizeNumber){
                                    resultOpenId = openId;
                                    resultFormId = formId;
                                    count = count + 1;
                                    // 用于拼接当前formID 发送次数
                                    useNumber = String.valueOf(count);
                                }
                            }
                            // 判断若FormId 的有效期在五天内，且发送次数小于给定次数
                            // 将FormId串中无效的formId排除，由于用户存入数据库
                            if(day < dayNumber && count < sizeNumber){
                                // 修改获取到的formId 发送通知次数信息
                                if(StringUtil.isNotEmpty(resultFormId)){
                                    formIdObj = (openId + FORMID_COLUMN_SPLIT + formId + FORMID_COLUMN_SPLIT + createTime + FORMID_COLUMN_SPLIT + count);
                                }
                                vipFormIdArray += StringUtil.isNotEmpty(vipFormIdArray)?("," + formIdObj):formIdObj;
                            }
                        }
                    }
                }
            }
        }catch (Exception e){
            throw new Exception("获取FORMID数据异常：" + e.getMessage());
        }finally {
            resultArray  = new String[]{resultOpenId, resultFormId, vipFormIdArray};
        }
        return resultArray;
    }


    /***
     * 获取时间距当前时间天数
     *
     * @param beginTimeValue
     * @return
     */
    public static int getTimeDay(String beginTimeValue){
        int resultDay = 0;
        Date beginTime  = DateUtil.turnStringToDate(beginTimeValue, "yyyy-MM-dd HH:mm:ss");
        Date currentTime = new Date();
        long currentLogin = currentTime.getTime();
        long beginLogin = beginTime.getTime();
        long value = currentLogin - beginLogin;
        resultDay = (int)(value / (1000 * 60 *60 * 24));
        return resultDay;
    }

    /**
     * 将使用天数转换为数值
     *
     * @param useNumber
     * @return
     */
    public static int getUseNumber(String useNumber){
        int resultUseNumber = 999;
        try{
            resultUseNumber = Integer.parseInt(useNumber);
        }catch (Exception e){ }
        return resultUseNumber;
    }

    public static void main(String[] args)throws Exception{
//        int day = getTimeDay("2018-10-20 12:00:00");
//        System.out.println("DAY_VALUE: " + day);
//        // "1541383354852_2018-10-29 00:00:00_0,1541394264370_2018-11-1 00:00:00_0,1541394269058_2018-10-28 00:00:00_0"
//        // 1541383354852        1541383997662       1541385011911
//        String params = "1541383354852_2018-10-29 00:00:00_0,1541394264370_2018-11-1 00:00:00_0,1541394269058_2018-11-05 00:00:00_0";
//        String[] content = getVipFormId(params ,6, 1);
//        System.out.println("CONTENT: " + content[0] + "  to  " + content[1]);

       String formIds =  getUserNewFormIds("1541401985187_2018-11-05 15:14:35_0,1541401987334_2018-11-05 15:14:48_0,1541401988321_2018-11-05 15:15:06_0,1541402008431_2018-11-05 15:15:35_0",
               "1541402009392", "");
       System.out.println("FORMIDS: " + formIds);

    }

}
